package com.xjgc.ws.util;

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.FileInputStream;

/**
 * jjg-pc.
 * 使用JAXP解析一份无效的XML文档(即满足相应DTD和Schema定义的元素规则)，
 */

public class CheckXml {

    //定义两个常量，注意：这两个常量是固定值
    //static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
    //static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";

    public String checkXml(String xml) throws Exception{
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        
        //builderFactory.setValidating(true);//设置解析器在解析文档的时候校验文档
        
        //builderFactory. setAttribute(JAXP_SCHEMA_LANGUAGE,W3C_XML_SCHEMA);//通过指定factory的属性，确定使用Schema进行校验
        
        //builderFactory.setNamespaceAware(true);//让解析器支持命名空间

        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        //添加ErrorHandler,将解析的异常手动抛出
        documentBuilder.setErrorHandler(new ErrorHandler() {
            @Override
            public void warning(SAXParseException exception) throws SAXException {
                System.out.println("warning");
                throw  exception;
            }
            @Override
            public void error(SAXParseException exception) throws SAXException {
                System.out.println("error");
                throw  exception;
            }
            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
                System.out.println("fatal");
                throw  exception;
            }
        });
        
        //解析文档
        Document document = documentBuilder.parse(new FileInputStream(xml));
        System.out.println(" &&&&&&&&&&&&&&&&&&&&&&&&&&&&     "+document);
        
        return "";
    }
	
}


